package com.xudow.app.ui.user;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.xudow.app.R;
import com.xudow.app.dynamicstate_old.module.collect.CollectDynamicFragment;
import com.xudow.app.ui.BaseActivity;
import com.xudow.app.ui.widget.XViewPager;

/**
 * 我的收藏Activity
 *
 * @author lukecheng
 */
public class FavoriteActivity extends BaseActivity {
    private static final int NUM_ITEMS = 2;

    private SectionsPagerAdapter pagerAdapter;
    private XViewPager viewPager;

    private TextView courseTab, otherTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite);

        courseTab = (TextView) findViewById(R.id.course_tab);
        otherTab = (TextView) findViewById(R.id.other_tab);

        pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        viewPager = (XViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                changeTabSelectionState(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void changeTabSelectionState(int position) {
        courseTab.setBackgroundResource(0);
        courseTab.setTextColor(getResources().getColor(R.color.text_medium));
        otherTab.setBackgroundResource(0);
        otherTab.setTextColor(getResources().getColor(R.color.text_medium));

        switch (position) {
            case 0:
                //courseTab.setBackgroundResource(R.mipmap.tab_selected_bottom_border);
                courseTab.setTextColor(getResources().getColor(R.color.rvcode));
                break;
            case 1:
                //otherTab.setBackgroundResource(R.mipmap.tab_selected_bottom_border);
                otherTab.setTextColor(getResources().getColor(R.color.rvcode));
                break;
            default:
                break;
        }
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    public void onTabClick(View view) {
        int index = 0;

        switch (view.getId()) {
            case R.id.course_tab:
                index = 0;
                break;
            case R.id.other_tab:
                index = 1;
                break;
        }

        changeTabSelectionState(index);
        viewPager.setCurrentItem(index);
    }

    public static class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;

            if (0 == position) {
                f = FavoriteCourseListFragment.newInstance();
            } else if (1 == position) {
                f = CollectDynamicFragment.newInstance();
            }

            return f;
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }
    }

}
